#!/bin/bash
tmp_json="/tmp/nic_json.json"
category="nic"

function getJsonValue(){
    grepKey=$1

    returnValue=`cat $tmp_json  |jq "${grepKey}" `


    keyType=`cat $tmp_json  |jq "${grepKey}" | jq type`
    if [  $keyType == '"array"' ];then
        returnValue=`echo $returnValue |jq .[0]`
    fi


    echo $returnValue | sed 's/"//g'


}


function main(){
    lshw  -class network -json >$tmp_json

    nicTotals=`cat $tmp_json |jq ".| length"`
    for ((i=0; i<$nicTotals; i++));
    do
        returnValue=`getJsonValue ".[$i].class" $tmp_json`

        if [ "$returnValue" != "network" ];then
            continue
        fi

        nic=`getJsonValue ".[$i].logicalname"`


        ## disable for IB drvier
        #if [ "$nic" == "null" ];then
        #    continue
        #fi

        nic_model=`getJsonValue ".[$i].product"`
        nic_brand=`getJsonValue ".[$i].vendor"`
        nic_mac=`getJsonValue ".[$i].serial"`
        nic_speed=`getJsonValue ".[$i].configuration.speed"`
        nic_status=`getJsonValue ".[$i].configuration.link"`
        nic_ip=`getJsonValue ".[$i].configuration.ip"`
        nic_firmwareversion=`getJsonValue ".[$i].configuration.firmware"`


        nic_slot=`getJsonValue ".[$i].businfo" $tmp_json`
        nic_slot=`echo $nic_slot | awk -F'@' '{print $2}'`


        if [ -f /sys/class/net/${nic}/device/uevent  ];then

            pci_SubsysId=`cat /sys/class/net/${nic}/device/uevent  |grep PCI_SUBSYS_ID |awk -F'=' '{print $2}'`
            nic_deviceid="0x"`cat /sys/class/net/${nic}/device/uevent  |grep "PCI_ID" |awk -F':' '{print $2}'`
        fi
        echo "category=${category};model=${nic_model};brand=${nic_brand};lable=${nic};slot=${nic_slot};mac=${nic_mac};speed=${nic_speed};status=${nic_status};ip=${nic_ip};subsysid=${pci_SubsysId};deviceid=${nic_deviceid};firmware_version=${nic_firmwareversion}"

    done

}

main

exit